package appBCN.version;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;
import appBCN.MOs.Person;
import appBCN.MOs.Pill;

public class HomeActivity extends Activity {
	private Person user;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        user = AppBCNApplication.getInstance().getPerson();
        
		setupTabHost();
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
		
		setupTab(new TextView(this), "Inici");
		setupTab(new TextView(this), "Perfil");
		setupTab(new TextView(this), "Medicines");
		setupTab(new TextView(this), "Doctor");
    }
    
    private TabHost mTabHost;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	private void setupTab(final View view, final String tag) {
		Resources res = getResources();
		View tabview = createTabView(mTabHost.getContext(), tag);
		TabSpec setContent = mTabHost.newTabSpec(tag);
		if(tag.equals("Inici")){
			setContent = mTabHost.newTabSpec(tag).setIndicator(tag,
                    res.getDrawable(R.drawable.ic_tabs))
                    .setContent(R.id.tabview1);
			
		}
		else if(tag.equals("Perfil")){
			TextView txtName = (TextView)findViewById(R.id.userName);
			TextView txtSurname = (TextView) findViewById(R.id.userCognoms);
			TextView txtAddress = (TextView) findViewById(R.id.userAddress);
			TextView txtCIP = (TextView) findViewById(R.id.userCIP);
			TextView txtDNI = (TextView) findViewById(R.id.userDNI);
			TextView txtTlf = (TextView) findViewById(R.id.userTlf);
			TextView txtEmail = (TextView) findViewById(R.id.userEmail);
			txtName.setText("Nom: "+user.getName());
			txtSurname.setText("Cognoms: "+user.getSurname());
			txtAddress.setText("Direccio: "+user.getAddress());
			txtCIP.setText("CIP: "+user.getCip());
			txtDNI.setText("DNI: "+user.getDni());
			txtTlf.setText("Tlf: "+user.getTelephone());
			txtEmail.setText("Email:"+user.getEmail());
			
			
			setContent = mTabHost.newTabSpec(tag).setIndicator(tag,
                    res.getDrawable(R.drawable.ic_tabs_profile)).setContent(R.id.tabview2);
		}
		else if(tag.equals("Medicines")){
			TextView txt=(TextView)findViewById(R.id.titlePills);
			txt.setText("Medicinas");
	    	user = AppBCNApplication.getInstance().getPerson();
			List<Pill>pills = new ArrayList<Pill>();
			if(user.getPills().size()<=0)
			{
				for(int i=2; i<10; i++)
				{
					Pill nPill = new Pill();
					nPill.setBarCode("01209302394"+i);
					nPill.setDescription("Prendre cadascuna "+i+" hores");
					nPill.setId("12312"+i);
					nPill.setName("Xvar "+i);
					nPill.setPrice(i+"0");
					pills.add(nPill);
				}
				user.setPills(pills);
				AppBCNApplication.getInstance().setPerson(user);
			}
			else
				pills = user.getPills();
	        ArrayAdapter ad=new ArrayAdapter(this, R.layout.list_item, pills);
	        final ListView listView=(ListView)findViewById(R.id.listPills);
	        listView.setAdapter(ad);
	        listView.setCacheColorHint(Color.TRANSPARENT);
	        setContent = mTabHost.newTabSpec(tag).setIndicator(tag,
                    res.getDrawable(R.drawable.ic_tabs_medicaments)).setContent(R.id.tabview3);
		}
		else if(tag.equals("Doctor")){
			TextView txtDName = (TextView)findViewById(R.id.doctorName);
			TextView txtDSurname = (TextView) findViewById(R.id.doctorCognoms);
			TextView txtDAddress = (TextView) findViewById(R.id.doctorAddress);
			TextView txtDTlf = (TextView) findViewById(R.id.doctorTlf);
			TextView txtDEmail = (TextView) findViewById(R.id.doctorEmail);
			TextView txtNName = (TextView) findViewById(R.id.nurseName);
			TextView txtNSurname = (TextView) findViewById(R.id.nurseCognoms);
			TextView txtNAddress = (TextView) findViewById(R.id.nurseAddress);
			TextView txtNTlf = (TextView) findViewById(R.id.nurseTlf);
			TextView txtNEmail = (TextView) findViewById(R.id.nurseEmail);
			txtDName.setText("Nom: "+user.getDoctor().getName());
			txtDSurname.setText("Cognoms: "+user.getDoctor().getSurname());
			txtDAddress.setText("Direccio: "+user.getDoctor().getAddress());
			txtDTlf.setText("Tlf: "+user.getDoctor().getTelephone());
			txtDEmail.setText("Email:"+user.getDoctor().getEmail());
			txtNName.setText("Nom: "+user.getNurse().getName());
			txtNSurname.setText("Cognoms: "+user.getNurse().getSurname());
			txtNAddress.setText("Direccio: "+user.getNurse().getAddress());
			txtNTlf.setText("Tlf: "+user.getNurse().getTelephone());
			txtNEmail.setText("Email:"+user.getNurse().getEmail());
			setContent = mTabHost.newTabSpec(tag).setIndicator(tag,
                    res.getDrawable(R.drawable.ic_tabs_doctor)).setContent(R.id.tabview4);
		}
		mTabHost.addTab(setContent);
	}

	private static View createTabView(final Context context, final String text) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		ImageView img = (ImageView)view.findViewById(R.id.tabsImage);
		tv.setText(text);
		return view;
	}
    
    public void onCreateAppointment_click(View view)
    {
    	Intent intent= new Intent( view.getContext(), SimpleCalendarViewActivity.class) ;
    	startActivity( intent );
    }
    
    public void onSeeAppointments_click(View view)
    {
    	Intent intent= new Intent( view.getContext(), SeeDates.class) ;
    	startActivity( intent );
    }
}
